package Algorithm.math;

/**
 * @author liujun
 * @version 1.0
 * @date 2020/5/15
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 大整数减法
 * 1、输入检查
 * 2、符号判断
 * 3、关于0的处理【0的处理需要考虑负号，因此放在符号判断以后】
 * 4、进行运算
 * 5、
 * 6、
 */

public class ArrayBigIntegersSub extends ArrayBigIntegersOperation {

    //大整数减法
    public String subWithSymbol(String args, String args2) {

        String sub = "0";
        // 输入检查
        if (!OperationCheck(args, args2)) {
            return sub;
        }
        // 符号判断
        boolean signal = true;
        boolean signal2 = true;
        if (args.charAt(0) == '-') {
            args = args.substring(1);
            signal = false;
        }
        if (args2.charAt(0) == '-') {
            args2 = args2.substring(1);//tips:substring是左闭右开
            signal2 = false;
        }
        // 关于0的处理
        if (args.equals("0") || args2.equals("0")) {
            if (args.equals("0")) {//tips:注意0和负数的情况
                return !signal2 || args2.equals("0") ? args2 : "-" + args2;
            }
            return signal ? args : "-" + args;
        }

        // 小数的处理
        int position = 0;
        if (args.contains(".") || args2.contains(".")) {
            int pointPosition = args.indexOf(".");
            int pointPosition2 = args2.indexOf(".");

            // 删除小数点
            StringBuilder argsBuilder = new StringBuilder(args);
            if (pointPosition != -1) {
                argsBuilder.deleteCharAt(pointPosition);
            } else {
                pointPosition = args.length() - 1;
            }
            StringBuilder argsBuilder2 = new StringBuilder(args2);
            if (pointPosition2 != -1) {
                argsBuilder2.deleteCharAt(pointPosition2);
            } else {
                pointPosition2 = args2.length();
            }

            // 注意此处是 argsBuilder.length
            while (pointPosition < argsBuilder.length() || pointPosition2 < argsBuilder2.length()) {
                if (pointPosition >= argsBuilder.length()) {
                    argsBuilder.append("0");
                }
                if (pointPosition2 >= argsBuilder2.length()) {
                    argsBuilder2.append("0");
                }
                position++;
                pointPosition++;
                pointPosition2++;
            }
            args = argsBuilder.toString();
            args2 = argsBuilder2.toString();
        }

        // 进行运算：两个正数相减、正数减负数、负数减正数、负数减负数
        if (signal) {
            if (signal2) {
                sub = subWithoutSymbol(args, args2);//两个正数相减
            } else {
                sub = sumWithoutSymbol(args, args2);//正数减负数
            }
        } else {
            if (signal2) {
                sub = "-" + sumWithoutSymbol(args, args2);//负数减正数
            } else {
                sub = subWithoutSymbol(args2, args);//负数减负数
            }
        }

        // 还原小数
        if (position != 0) {
            StringBuilder sb = new StringBuilder(sub);
            sb.insert(sub.length() - position, ".");
            int length = sb.length() - 1;
            while (sb.charAt(length) != '.' && sb.charAt(length) == '0') {
                sb.deleteCharAt(length);
                length--;
            }
            // 注意最后一个数字是"."
            sub = sb.charAt(length) != '.' ? sb.toString() : sb.deleteCharAt(length).toString();
        }
        return sub;
    }
}
